<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.title"
        placeholder="标题名"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.template"
        placeholder="表单类型"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option v-for="item in allTemplates" :key="item.id" :label="item.name" :value="item.id" />
      </el-select>
      <el-select
        v-model="listQuery.formStatus"
        placeholder="表单状态"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option
          v-for="item in form_status"
          :key="item.key"
          :label="item.display_name"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="listQuery.checkStatus"
        placeholder="审批状态"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option
          v-for="item in check_state"
          :key="item.key"
          :label="item.display_name"
          :value="item.key"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
    </div>
    <el-table v-loading="listLoading" :data="allOutlines" border fit highlight-current-row style="width: 100%;">
      <el-table-column label="编号" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="表单类型" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ templateType(row.template_id) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="300px" align="center">
        <template slot-scope="{row}">
          <span>{{ change(row.create_time) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="表单状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag>{{ row.status | statusFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="审批状态" class-name="status-col" width="100">
        <template slot-scope="{row}">
          <el-tag>{{ row.check_status | checkFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="Actions"
        align="center"
        width="230"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="{row,$index}">
          <el-button
            class="mgr"
            type="primary"
            icon="el-icon-edit"
            circle
            @click="handleUpdate(row)"
          />
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDelete(row, $index)" />
        </template>
      </el-table-column>
    </el-table>
    <pagination v-show="total>0" :total="total" :page.sync="listQuery.page" :limit.sync="listQuery.limit" @pagination="getList" />
    <el-dialog :title="current_temple.name" :visible.sync="dialogVisible" width="70%" top="5vh">
      <dialogform ref="dialogForm" />
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="handlerApply(current_detail.id)">申请</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { getAllOutline, getAllTemplates, getTemplate, getOutlineWithData, deleteOutline, onOutlineApply } from '@/api/customForm'
import waves from '@/directive/waves'
import dialogform from '../components/dialogForm'
import Pagination from '@/components/Pagination'
import { copy as util_Copy } from '@/utils/clone'
const form_status = [
  { key: 'w', display_name: '未申请' },
  { key: 'o', display_name: '已申请' },
  { key: 'e', display_name: '已结束' }
]
const check_state = [
  { key: 'w', display_name: '等待审批' },
  { key: 'o', display_name: '审批中' },
  { key: 's', display_name: '通过' },
  { key: 'f', display_name: '拒绝' }
]
const calendarFormStateValue = form_status.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})
const calendarCheckStateValue = check_state.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name
  return acc
}, {})
export default {
  components: { Pagination, dialogform },
  directives: { waves },
  filters: {
    statusFilter(status) {
      return calendarFormStateValue[status]
    },
    checkFilter(status) {
      return calendarCheckStateValue[status]
    },
    typeFilter(type) {
      return this.allTemplates.find(e => e.id === type).name
    }
  },
  data() {
    return {
      allOutlines: null,
      allTemplates: null,
      current_temple: {
        name: ''
      },
      current_detail: null,
      form_status,
      check_state,
      listLoading: true,
      titleMap: {
        update: '更新',
        create: '创建'
      },
      dialogVisible: false,
      dialogStatus: null,
      temp: {
        id: undefined,
        name: '',
        description: '',
        status: ''
      },
      listQuery: {
        title: '',
        page: 1,
        limit: 10,
        template: undefined,
        formStatus: undefined,
        checkStatus: undefined
      },
      total: 0
    }
  },
  computed: {
    templateType: function() {
      return function(id) {
        return this.allTemplates.find(t => t.id === id).name
        // console.log(this.allTemplates)
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    /**
         * 初始化数据
         */
    initData() {
      getAllTemplates().then(res => {
        this.allTemplates = res.data
      })
      this.getList()
    },
    getList() {
      this.listLoading = true
      getAllOutline(this.listQuery).then(res => {
        this.total = res.data.total
        this.allOutlines = res.data.records
        this.listLoading = false
      })
    },

    /**
         * 打开更新窗口
         * @param {*} row
         */
    handleUpdate(row) {
      getTemplate(row.template_id).then(res => {
        this.current_temple = res.data
        getOutlineWithData(row.id).then(res => {
          this.current_detail = res.data
          this.showDialog()
        })
      })
    },
    /**
       * 删除
       * @param {*} row
       * @param {*} index
       */
    handleDelete(row, index) {
      deleteOutline(row.id).then(res => {
        this.$notify({
          title: '消息',
          message: res.data,
          type: 'success',
          duration: 2000
        })
        this.allOutlines.splice(index, 1)
      })
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    change(time) {
      return new Date(time).toLocaleString()
    },
    showDialog() {
      // 弹出预览框
      this.dialogVisible = true
      this.current_temple.structure
      // 异步调用弹框的方法
      setTimeout(() => {
        // 异步调用弹框中的方法传递表单数据
        const structure = JSON.parse(this.current_temple.structure)
        const my_data = Object.values(JSON.parse(this.current_detail.data_list))
        for (let i = 0; i < Object.getOwnPropertyNames(structure.list).length - 1; i++) {
          structure.list[i].options.defaultValue = my_data[i]
        }
        this.$refs.dialogForm.setFormVal(util_Copy(structure))
      })
    },
    handlerApply(id) {
      console.log(id)
      onOutlineApply(id).then(res => {
        console.log(res.data)
      })
    }
  }
}
</script>

<style>
.mgr {
  margin-right: 20px;
}
</style>
